package com.cash.loan.modular.cash.controller;

import com.cash.loan.common.annotation.UserLogin;
import com.cash.loan.common.entity.UserInfo;
import com.cash.loan.common.exception.BizExceptionEnum;
import com.cash.loan.common.result.PageResult;
import com.cash.loan.common.result.ServerResponse;
import com.cash.loan.core.base.controller.BaseController;
import com.cash.loan.modular.cash.annotation.PvUvLog;
import com.cash.loan.modular.cash.service.ProductService;
import com.cash.loan.modular.cash.transfer.vo.ProductDetailMapVo;
import com.cash.loan.modular.cash.transfer.vo.ProductListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.HashMap;

/**
 * @author Administrator
 * @Date 2018-07-06 14:19
 */
@Api(description = "产品接口")
@RestController
@RequestMapping(value = "/product")
public class ProductController extends BaseController{

    private static Logger logger = LoggerFactory.getLogger(ProductController.class);

    private static final String BETWEEN_CHAR = "-";

    private static final String MORE_CHAR = "\\+";

    @Autowired
    private ProductService productService;


    /**
     * 产品列表查询
     * @param currentPage 当前页
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation("产品列表")
    @GetMapping(value = "/list")
    public ServerResponse getProductList(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                         @RequestParam(value = "loanAmount",required = false) String loanAmout,
                                         @RequestParam(value = "loanTerm",required = false) String loanTerm,
                                         @RequestParam(value = "orderBy",required = false) String orderBy,
                                         @RequestParam(value = "cateId",required = false) Integer cateId,
                                         @RequestHeader(value = "uitype",required = false,defaultValue = "0") Integer uiType) {
        Integer minLoanTerm = null;
        Integer maxLoanTerm = null;
        BigDecimal minLoanAmount = null;
        BigDecimal maxLoanAmount = null;
        if(StringUtils.isNoneBlank(loanTerm)) {
            if(loanTerm.contains(BETWEEN_CHAR)) {
                minLoanTerm = Integer.valueOf(loanTerm.split(BETWEEN_CHAR)[0]);
                maxLoanTerm = Integer.valueOf(loanTerm.split(BETWEEN_CHAR)[1]);
            }
            if(loanTerm.contains("+")) {
                minLoanTerm = Integer.valueOf(loanTerm.split("\\+")[0]);
            }
        }
        if(StringUtils.isNoneBlank(loanAmout)) {
            if(loanAmout.contains(BETWEEN_CHAR)) {
                minLoanAmount = BigDecimal.valueOf(Double.valueOf(loanAmout.split(BETWEEN_CHAR)[0]));
                maxLoanAmount = BigDecimal.valueOf(Double.valueOf(loanAmout.split(BETWEEN_CHAR)[1]));
            }
            if(loanAmout.contains("+")) {
                minLoanAmount = BigDecimal.valueOf(Double.valueOf(loanAmout.split("\\+")[0]));
            }
        }
        PageResult<ProductListVo> pageResult = productService.getProductPageList(currentPage,pageSize,
                minLoanTerm,maxLoanTerm,minLoanAmount,maxLoanAmount,orderBy,cateId,uiType);
        return ServerResponse.getSuccess("success",pageResult);
    }

    /**
     * 详情页面 待定，看是否需要和申请页面合并
     * @param productId
     * @return
     */
    @PvUvLog
    @ApiOperation("产品详情页")
    @GetMapping(value = "/detail")
    public ServerResponse getProductDetail(@RequestParam(value = "productId") Integer productId,
                                           @RequestHeader(value = "Access-Token",required = false,defaultValue = "no-token") String token,
                                           @UserLogin UserInfo userInfo) {
        ProductDetailMapVo mapVo = productService.productDetail(productId);
        if(mapVo == null) {
            return ServerResponse.getSuccess("success",new HashMap<>(1));
        }
        return ServerResponse.getSuccess("success",mapVo);
    }

    @ApiOperation("点击申请统计")
    @PvUvLog(type = "1")
    @GetMapping(value = "/toApply")
    public ServerResponse toApplyPage(@RequestParam(value = "productId") Integer productId,
                                      @RequestHeader(value = "Access-Token",required = false,defaultValue = "no-token") String token,
                                      @UserLogin UserInfo userInfo) {
        String url = productService.getApplyPageUrl(productId);
        Map<String,String> result = new HashMap<>(1);
        result.put("applyUrl",url);
        return ServerResponse.getSuccess("success",result);
    }


    @ApiOperation("点击申请统计-需要用户登录")
    @PvUvLog(type = "1")
    @GetMapping(value = "/toApplyLog")
    public ServerResponse toApplyPageLog(@RequestParam(value = "productId") Integer productId,
                                      @RequestHeader(value = "Access-Token",required = false,defaultValue = "no-token") String token,
                                      @UserLogin UserInfo userInfo) {
        if(userInfo == null) {
            return ServerResponse.getErrorCodeMessage(BizExceptionEnum.TOKEN_ERROR.getCode(),"用户未登录");
        } else {
            String url = productService.getApplyPageUrl(productId);
            Map<String,String> result = new HashMap<>(1);
            result.put("applyUrl",url);
            return ServerResponse.getSuccess("success",result);
        }
       
    }
}
